package com.cloud.drore.eboos.common.util;

import com.cloud.drore.eboos.exception.BusinessException;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 浙江卓锐科技股份有限公司 版权所有  Copyright 2018<br>
 *
 * @Description: <br>
 * @Project: eboos <br>
 * @CreateDate: Created in 2018/4/11 19:23 <br>
 * @Author: <a href="1451173193@qq.com">杨亮</a>
 */
public class CalendarUtil {
    private static final String dateFormat = "yyyy-MM-dd";
    //以礼拜一为每周的第一天
    public static final int LAST_MONDAY = 6;//上个礼拜一
    public static final int LAST_SUNDAY = 0;//上个礼拜天
    private static final String DATE_ERROR="时间有误！";
    /*以礼拜日为每周的第一天
    public static final int LAST_SUNDAY=7;
    public static final int LAST_SATURDAY=1;*/

    /**
     * @param oneDay
     * @ClassName: CalendarUtil
     * @Return: java.lang.String
     * @Decription:获取上一周的第一天或者最后一天日期
     * @CreateDate: Created in 2018/4/11 20:21
     * @Author: <a href="1451173193@qq.com">杨亮</a>
     * @Modify:
     */
    public static String getOneDayOfLastWeek(int oneDay) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat);
        LocalDate localDate = LocalDate.now();
        int dayValue = localDate.getDayOfWeek().getValue() + oneDay;
        return localDate.minusDays(dayValue).format(formatter);
    }
    /**
     * @ClassName: CalendarUtil
     * @param startDate
     * @param endDate
     * @Return: java.util.List<java.time.LocalDate>
     * @Decription:获取俩个date之间的所有日期返回集合
     * @CreateDate: Created in 2018/4/12 15:29
     * @Author: <a href="1451173193@qq.com">杨亮</a>
     * @Modify:
     */
    public static List<String> getDateListBetweenTwoDays(Date startDate, Date endDate) throws BusinessException {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat);
        List<String> dateList=new ArrayList<>();
        LocalDate now=LocalDate.now();
        LocalDate localDateStart=startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate localDateEnd=endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        /*防止参数异常，导致服务宕机，最多只能取近一年的每天数据*/
        if(localDateEnd.compareTo(localDateStart)<0||now.plusYears(1).compareTo(localDateEnd)<0){
            throw new BusinessException(DATE_ERROR);
        }
        while(!localDateStart.isEqual(localDateEnd)){
            dateList.add(localDateStart.format(formatter));
            localDateStart=localDateStart.plusDays(1);
        }
        dateList.add(localDateEnd.format(formatter));
        return dateList;
    }
    /**
     * @ClassName: CalendarUtil
     * @param endDate
     * @Return: java.util.List<java.lang.String>
     * @Decription:获取从今天开始一周的时间集合
     * @CreateDate: Created in 2018/4/13 14:00
     * @Author: <a href="1451173193@qq.com">杨亮</a>
     * @Modify:
     */
    public static List<String> getNextWeekDateStrListFromToday(){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat);
        List<String> dateList=new ArrayList<>();
        LocalDate today=LocalDate.now();
        dateList.add(today.format(formatter));
        for(int i=1;i<7;i++){
            today=today.plusDays(1);
            dateList.add(today.format(formatter));
        }
        return dateList;
    }
}
